/*
    Document   : account.js
    Created on : Nov 21, 2012
    Author     : HongDC
*/
var user = "";
var xmlhttp;

$("#register").addEventListener('submit', function(e) {
    var form = $("#register");
    var username = form.elements["username"].value;
    var password = form.elements["password"].value;
    var email = form.elements["email"].value;

    xmlhttp = setXmlHttpRequest();

    xmlhttp.open("POST","AccountController?action=Register", true);

    var params = "username="+username+"&email="+email+"&password="+password;
    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            xmlResult = parseXml(xmlhttp.responseText);
            response = xmlResult.documentElement;
            if (response.childNodes[0].textContent == "OK") {
                user = response.childNodes[1].textContent;
                $(".accStt").innerHTML = "Welcome, "+user+" | " +
                    '<a id="userLink" href="javascript:void()" onclick="logout()">Logout</a>';

                toggleDialog("#dAccount");
                alert("Hello "+user+".\nYou're now login.");
            } else {
                alert(response.childNodes[1].textContent);
            }
            sessionStorage.user = user;
        }
    }
    clearInput();
    e.preventDefault();
});

$("#login").addEventListener('submit', function(e) {
    var form = $("#login");
    var username = form.elements["username"].value;
    var password = form.elements["password"].value;

    xmlhttp = setXmlHttpRequest();

    xmlhttp.open("POST","AccountController?action=Login", true);

    var params = "username="+username+"&password="+password;
    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            xmlResult = parseXml(xmlhttp.responseText);
            response = xmlResult.documentElement;
            if (response.childNodes[0].textContent == "OK") {
                user = response.childNodes[1].textContent;
                $(".accStt").innerHTML = "Welcome, "+user+" | " +
                    '<a href="javascript:void()" onclick="logout()">Logout</a>';
                sessionStorage.user = user;
                toggleDialog("#dAccount");
                alert("Hello "+user+".\nYou're now login.");
            } else {
                alert(response.childNodes[1].textContent);
            }
            
        }
    }
    clearInput();
    e.preventDefault();
});

function logout() {
    xmlhttp = setXmlHttpRequest();
    
    xmlhttp.open("POST","AccountController?action=Logout", true);
    xmlhttp.send();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            xmlResult = parseXml(xmlhttp.responseText);
            response = xmlResult.documentElement;
            if (response.childNodes[0].textContent == "OK") {
                user = response.childNodes[1].textContent;
                $(".accStt").innerHTML = '<a href="javascript:void()" onclick="toggleDialog(\'#dAccount\')">Register or login</a>';
                alert("Goodbye "+user+".\nHope to see you soon.");
            } else {
                alert(response.childNodes[1].textContent);
            }
            sessionStorage.user = "";
            makeStorage("remove");
            mapCart();
            location.reload();
        }
    }
}

function clearInput() {
    $("#register").elements["username"].value = "";
    $("#register").elements["email"].value = "";
    $("#register").elements["password"].value = "";
    $("#login").elements["username"].value = "";
    $("#login").elements["password"].value = "";
}

function checkLogin() {
    if (typeof sessionStorage.user == "undefined" || sessionStorage.user == "") {
        return false;
    } else {
        return true;
    }
}